home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 19 / Mac Magazin and MacEasy Magazine CD - Issue 19.iso / Wissenschaft & Technik / BibTeX ƒ / Source code / CBibTeXApp.cp < prev    next >
Text File  |  1996-02-18  |  50KB  |  999 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. * Product:    Bibtex                                                      *
  4. * Class:      CBibtexApp                                                  *
  5. * Superclass: LDocApplication                                             *
  6. * File:       CBibTeXApp.cp                                               *
  7. * Abstract:   This file overrides the standard PowerPlant application to  *
  8. *             provide the features that we need for Bibtex.               *
  9. * History:                                                                *
  10. *     1.0MI   by Rick Zaccone for MakeIndex                                  *
  11. *     1.0     1-Feb-96   VMD.  Initial version - basically a global       *
  12. *                        replacement of Rick's 'Makeindex' with 'BibTeX'  *
  13. *                                                                         *
  14. **************************************************************************/
  15.  
  16. /* There is some old stuff in here from Makeindex which needs tidying up */
  17.  
  18. #include "CBibTeXApp.h"
  19. #include "BibTeXConstants.h"
  20. #include "BibTeXApp.h"
  21. #include "StdPrefsLib.h"
  22.  
  23. #include <LEditField.h>
  24. #include <LGrowZone.h>
  25. #include <LWindow.h>
  26. #include "bibutils.h"
  27. #include <PP_Messages.h>
  28. #include <PP_Resources.h>
  29. #include <PPobClasses.h>
  30. #include <string.h>
  31. #include <UDrawingState.h>
  32. #include <UMemoryMgr.h>
  33. #include <URegistrar.h>
  34.  
  35. // Static data members:
  36.  
  37. FileFilterUPP   CBibtexApp::sGetBtxFileFilter = NULL;
  38. TEHandle        CBibtexApp::sTextEditH;
  39. LTextEdit       *CBibtexApp::sTextEditPane;
  40.  
  41. #undef __profile__
  42.  
  43. #ifdef __profile__
  44. #include <profiler.h>
  45. #pragma profile off
  46.  
  47. #endif
  48.  
  49. CBibtexApp* theAppPointer;
  50.  
  51. /**************************************************************************
  52. *                                                                         *
  53. * Function:   main                                                        *
  54. * Purpose:    Initializes the heap, the toolbox and installs a grow zone  *
  55. *             function.  Finally, it creates an application object and    *
  56. *             then runs it.                                               *
  57. * Inputs:     None.                                                       *
  58. * Outputs:    None.                                                       *
  59. * Exceptions: None.                                                       *
  60. * Side Effects:   Creates the application object.                         *
  61. * Errors:     None.                                                       *
  62. * History:                                                                *
  63. *     1.0     17-Jul-95   RJZ.  Initial version.                          *
  64. *                                                                         *
  65. **************************************************************************/
  66. void main(void) {
  67.     #ifdef __profile__
  68.     ProfilerInit(collectDetailed, bestTimeBase, 100, 10);
  69.     #endif
  70.  
  71.                                         // See UDebugging.h for how these
  72.                                         // macros work.  Use PP_DebugHeaders68K
  73.                                         // in the C/C++ Language panel when
  74.                                         // debugging and to see error alerts.
  75.                                         // Use PP_ClassHeaders68K when done
  76.                                         // debugging.
  77.     SetDebugThrow_(debugAction_Alert);
  78.     SetDebugSignal_(debugAction_Alert);
  79. //    SetDebugThrow_(debugAction_Nothing);
  80. //    SetDebugSignal_(debugAction_Nothing);
  81.  
  82.                                         // Initialize Memory Manager.
  83.                                         // Parameter is number of Master
  84.                                         // Pointer blocks to allocate
  85.     InitializeHeap(3);
  86.                                         // Initialize standard Toolbox managers
  87.     UQDGlobals::InitializeToolbox(&qd);
  88.                                         // Install a GrowZone function to catch
  89.                                         // low memory situations.
  90.     new LGrowZone(20000);
  91.     
  92.     CBibtexApp theApp;
  93.     // so we can get events from outside calls
  94.     theAppPointer = &theApp;
  95.     theApp.Run();
  96. }
  97.  
  98. /**************************************************************************
  99. *                                                                         *
  100. * Method:     CBibtexApp                                                 *
  101. * Purpose:    This is the constructor for the application object.  It     *
  102. *             performs some initialization.  If a preferences file        *
  103. *             exists, it reads it.                                        *
  104. * Inputs:     None.                                                       *
  105. * Outputs:    None.                                                       *
  106. * Exceptions: None.                                                       *
  107. * Side Effects:   Registers all relevant PowerPlant classes.              *
  108. * Errors:     None.                                                       *
  109. * History:                                                                *
  110. *     1.0     17-Jul-95   RJZ.  Initial version.                          *
  111. *     1.0.1   27-Oct-95   RJZ.  Make the UPP a static data member.        *
  112. *     2.0      6-Feb-96      VMD.  For BibTeX                                                                    *
  113. *                                                                         *
  114. **************************************************************************/
  115. CBibtexApp::CBibtexApp() {
  116.     Boolean         debugOn;
  117.     OSErr           err;
  118.     short           fileRefNum;
  119.  
  120.                                         // Register functions to create core
  121.                                         // PowerPlant classes
  122.     //URegistrar::RegisterClass(LButton::class_ID,        LButton::CreateButtonStream);
  123.     URegistrar::RegisterClass(LCaption::class_ID,        LCaption::CreateCaptionStream);
  124.     URegistrar::RegisterClass(LDialogBox::class_ID,        LDialogBox::CreateDialogBoxStream);
  125.     //URegistrar::RegisterClass(LEditField::class_ID,        LEditField::CreateEditFieldStream);
  126.     //URegistrar::RegisterClass(LListBox::class_ID,        LListBox::CreateListBoxStream);
  127.     //URegistrar::RegisterClass(LPane::class_ID,            LPane::CreatePaneStream);
  128.     //URegistrar::RegisterClass(LPicture::class_ID,        LPicture::CreatePictureStream);
  129.     //URegistrar::RegisterClass(LPlaceHolder::class_ID,    LPlaceHolder::CreatePlaceHolderStream);
  130.     //URegistrar::RegisterClass(LPrintout::class_ID,        LPrintout::CreatePrintoutStream);
  131.     //URegistrar::RegisterClass('prto',                    LPrintout::CreateOldPrintoutStream);
  132.     URegistrar::RegisterClass(LScroller::class_ID,        LScroller::CreateScrollerStream);
  133.     //URegistrar::RegisterClass(LStdControl::class_ID,    LStdControl::CreateStdControlStream);
  134.     URegistrar::RegisterClass(LStdButton::class_ID,        LStdButton::CreateStdButtonStream);
  135.     URegistrar::RegisterClass(LStdCheckBox::class_ID,    LStdCheckBox::CreateStdCheckBoxStream);
  136.     //URegistrar::RegisterClass(LStdRadioButton::class_ID,LStdRadioButton::CreateStdRadioButtonStream);
  137.     //URegistrar::RegisterClass(LStdPopupMenu::class_ID,    LStdPopupMenu::CreateStdPopupMenuStream);
  138.     URegistrar::RegisterClass(LTextEdit::class_ID,        LTextEdit::CreateTextEditStream);
  139.     //URegistrar::RegisterClass(LView::class_ID,            LView::CreateViewStream);
  140.     URegistrar::RegisterClass(LWindow::class_ID,        LWindow::CreateWindowStream);
  141.  
  142.     //URegistrar::RegisterClass(LRadioGroup::class_ID,    LRadioGroup::CreateRadioGroupStream);
  143.     //URegistrar::RegisterClass(LTabGroup::class_ID,        LTabGroup::CreateTabGroupStream);
  144.  
  145.     //URegistrar::RegisterClass(LCicnButton::class_ID,    LCicnButton::CreateCicnButtonStream);
  146.     //URegistrar::RegisterClass(LOffscreenView::class_ID,    LOffscreenView::CreateOffscreenViewStream);
  147.     //URegistrar::RegisterClass(LActiveScroller::class_ID,LActiveScroller::CreateActiveScrollerStream);
  148.     //URegistrar::RegisterClass(LTable::class_ID,            LTable::CreateTableStream);
  149.     //URegistrar::RegisterClass(LIconPane::class_ID,        LIconPane::CreateIconPaneStream);
  150.     //URegistrar::RegisterClass(LGroupBox::class_ID,        LGroupBox::CreateGroupBoxStream);
  151.     //URegistrar::RegisterClass(LTextButton::class_ID,    LTextButton::CreateTextButtonStream);
  152.     
  153.     //RegisterAllPPClasses();
  154.                                         // no btx file yet
  155.     mGotBtxFile = false;
  156.     mLogWindow = NULL;
  157.     sGetBtxFileFilter = NewFileFilterProc((ProcPtr) GetBtxFileFilter);
  158.     ThrowIfMemError_();
  159.     err = ::OneTimeInitialization();
  160.     ThrowIfOSErr_(err);
  161.                                         // If the preferences file exists, read
  162.                                         // it and set the appropriate variables
  163.                                         // accordingly.
  164.  
  165.     mPrefsFileExists = PreferencesFileExists(kDummyCreator, kDummyType);
  166.     if (mPrefsFileExists) {
  167.         err = OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
  168.         if (err == noErr) {
  169.             ReadBooleanPref(debugOn,        myBLID_Debug,        fileRefNum);
  170.             ReadBooleanPref(mGoOnOpen,      myBLID_GoOnOpen,     fileRefNum);
  171.             ReadBooleanPref(mAutoQuit,      myBLID_AutoQuit,     fileRefNum);
  172.             SetBibtexOptions(debugOn);
  173.             err = ClosePreferencesFile(fileRefNum);
  174.         } else
  175.             ReportOSErr("Error trying to open preferences file: %s", err);
  176.  
  177.                                         // If the preferences file doesn\'t
  178.                                         // exist, use the default values for Go
  179.                                         // On Open and Auto Quit.
  180.     } else {
  181.         mGoOnOpen = kGoOnOpenDefault;
  182.         mAutoQuit = kAutoQuitDefault;
  183.     }
  184. }
  185.  
  186. /*********************************************************************
  187. *                                                                    *
  188. * Method:     ~CBibtexApp                                           *
  189. * Purpose:    Destructor for the application object.  It doesn't do  *
  190. *             anything yet.                                          *
  191. * Inputs:     None.                                                  *
  192. * Outputs:    None.                                                  *
  193. * Exceptions: None.                                                  *
  194. * Side Effects:   None.                                              *
  195. * Errors:     None.                                                  *
  196. * History:                                                           *
  197. *     1.0     17-Jul-95   RJZ.  Initial version.                     *
  198. *                                                                    *
  199. *********************************************************************/
  200. CBibtexApp::~CBibtexApp(void) {
  201. }
  202.  
  203. /************************************************************************
  204. *                                                                       *
  205. * Method:     StartUp                                                   *
  206. * Purpose:    This function lets you do something when the application  *
  207. *             starts up.  For example, you could issue your own New     *
  208. *             command.                                                  *
  209. * Inputs:     None.                                                     *
  210. * Outputs:    None.                                                     *
  211. * Exceptions: None.                                                     *
  212. * Side Effects:   None.                                                 *
  213. * Errors:     None.                                                     *
  214. * History:                                                              *
  215. *     1.0     17-Jul-95   RJZ.  Initial version.                        *
  216. *                                                                       *
  217. ************************************************************************/
  218. void CBibtexApp::StartUp(void) {
  219.  
  220. }
  221.  
  222. /**************************************************************************
  223. *                                                                         *
  224. * Method:     ObeyCommand                                                 *
  225. * Purpose:    Obeys commands for this application.  If the command is     *
  226. *             not a Bibtex specific command, call the overridden       *
  227. *             ObeyCommand.                                                *
  228. * Inputs:     inCommand is the command to be obeyed.  If the command is   *
  229. *             sent as a result of a menu selection, the ioParam           *
  230. *             parameter is an Int32*. The value is the result returned    *
  231. *             by MenuSelect or MenuKey, which has the MENU ID in the hi   *
  232. *             word and the menu item number in the lo word.  This allows  *
  233. *             you to use a "real" command number rather than relying on   *
  234. *             synthetic command numbers for menu choices that depend on   *
  235. *             the text of the menu item. For example, a Font or Size      *
  236. *             menu.                                                       *
  237. * Outputs:    ioParam may be an output parameter.  See the Inputs         *
  238. *             section.                                                    *
  239. * Exceptions: None.                                                       *
  240. * Side Effects:   Creates the Bibtex log window.                       *
  241. * Errors:     None.                                                       *
  242. * History:                                                                *
  243. *     1.0     17-Jul-95   RJZ.  Initial version.                          *
  244. *     1.0.1   23-Oct-95   RJZ.  Changed the default so that it calls      *
  245. *                         ObeyCommand for LDocApplication.  This allowed  *
  246. *                         me to get rid of the processing for cmd_Open.   *
  247. *                                                                         *
  248. **************************************************************************/
  249. Boolean CBibtexApp::ObeyCommand(CommandT inCommand, void *ioParam) {
  250.     Boolean     cmdHandled = true;
  251.  
  252.     switch (inCommand) {
  253.  
  254.     case cmd_Go:
  255.         ::InvokeBibtex();
  256.         if (mAutoQuit)
  257.             ObeyCommand(cmd_Quit, NULL);
  258.         break;
  259.  
  260.     case cmd_OpenOptionsDialog:
  261.         OpenOptionsDialog();
  262.         break;
  263.  
  264.     case cmd_SetOptions:
  265.         SetDialogOptions((SDialogResponse *) ioParam);
  266.         break;
  267.  
  268.     case cmd_SaveSettings:
  269.         DoSaveSettings();
  270.         break;
  271.  
  272.     default:
  273.         cmdHandled = LDocApplication::ObeyCommand(inCommand, ioParam);
  274.         break;
  275.     }
  276.  
  277.     return cmdHandled;
  278. }
  279.  
  280. /**************************************************************************
  281. *                                                                         *
  282. * Method:     FindCommandStatus                                           *
  283. * Purpose:    Pass back whether a menu Command is enabled and/or marked.  *
  284. * Inputs:     inCommand is a menu command number.                         *
  285. * Outputs:    outEnabled indicates if it is enabled, outUsesMark          *
  286. *             indicates if it is marked, outMark is the mark.  outName    *
  287. *             is the new name for the menu item.                          *
  288. * Exceptions: None.                                                       *
  289. * Side Effects:   None.                                                   *
  290. * Errors:     None.                                                       *
  291. * History:                                                                *
  292. *     1.0     17-Jul-95   RJZ.  Initial version.                          *
  293. *     1.0.1   23-Oct-95   RJZ.  The default case should call              *
  294. *                         LDocApplication::FindCommandStatus().           *
  295. *                                                                         *
  296. **************************************************************************/
  297. void CBibtexApp::FindCommandStatus(CommandT inCommand,
  298.                                     Boolean  &outEnabled,
  299.                                     Boolean  &outUsesMark,
  300.                                     Char16   &outMark,
  301.                                     Str255   outName)
  302. {
  303.     switch (inCommand) {
  304.                                         // Return menu item status according to
  305.                                         // command messages.  Any that you
  306.                                         // don't handle will be passed to
  307.                                         // LDocApplication
  308.     case cmd_New:
  309.         outEnabled = false;             // disable the New command
  310.         outUsesMark = false;
  311.         break;
  312.                                         // The Open command is available if
  313.                                         // there is no window open and the
  314.                                         // preferences dialog is not open.
  315.     case cmd_Open:
  316.         outEnabled = !UDesktop::FrontWindowIsModal() && mLogWindow == NULL;
  317.         break;
  318.                                         // The Close command is enabled if the
  319.                                         // front window is not modal and there
  320.                                         // is a log window open.
  321.     case cmd_Close:
  322.     case cmd_Go:
  323.         outEnabled = !UDesktop::FrontWindowIsModal() && mLogWindow != NULL;
  324.         break;
  325.                                         // The Options... command is available
  326.                                         // as long as the options dialog isn't
  327.                                         // already being displayed.  PowerPlant
  328.                                         // will probably do this automatically
  329.                                         // in a future version.  Same for Save
  330.                                         // command.
  331.     case cmd_OpenOptionsDialog:
  332.     case cmd_SaveSettings:
  333.     case cmd_Quit:
  334.         outEnabled = !UDesktop::FrontWindowIsModal();
  335.         break;
  336.  
  337.     default:
  338.         LDocApplication::FindCommandStatus(inCommand, outEnabled,
  339.                                         outUsesMark, outMark, outName);
  340.         break;
  341.     }
  342. }
  343.  
  344. /**********************************************************************
  345. *                                                                     *
  346. * Method:     CBibtexApp::DisplayInLogWindow (static)                *
  347. * Purpose:    Displays text in the log window.                        *
  348. * Inputs:     inMessage is a pointer to a string to be displayed.     *
  349. *             inSize is the size of the string.                       *
  350. * Outputs:    None.                                                   *
  351. * Exceptions: None.                                                   *
  352. * Side Effects:   Changes the contents of the Log Window.             *
  353. * Design:     Just uses toolbox routines to insert text into the log  *
  354. *             window.                                                 *
  355. * Errors:     None.                                                   *
  356. * History:                                                            *
  357. *     1.0     28-Jul-95   RJZ.  Initial version.                      *
  358. *                                                                     *
  359. **********************************************************************/
  360. void CBibtexApp::DisplayInLogWindow(char *inMessage, Int32 inSize) {
  361.     for(short i=0;i<strlen(inMessage);i++){
  362.         if (inMessage[i] == '\n')
  363.             inMessage[i] = '\r';
  364.     }
  365.     ::TEInsert(inMessage, inSize, sTextEditH);
  366.     ::TECalText(sTextEditH);
  367.     sTextEditPane->AdjustImageToText();
  368. }
  369.  
  370. /************************************************************************
  371. *                                                                       *
  372. * Method:     CBibtexApp::PostErrorAlert (static)                      *
  373. * Purpose:    Displays an error Alert.                                  *
  374. * Inputs:     inMessage is a pointer to a Pascal style string to be     *
  375. *             displayed.                                                *
  376. * Outputs:    None.                                                     *
  377. * Exceptions: None.                                                     *
  378. * Side Effects:   Displays an alert.                                    *
  379. * Design:     Deactivate and activate the desktop.  It doesn't seem to  *
  380. *             be necessary, but all the example programs do it!         *
  381. * Errors:     None.                                                     *
  382. * History:                                                              *
  383. *     1.0     28-Jul-95   RJZ.  Initial version.                        *
  384. *                                                                       *
  385. ************************************************************************/
  386. void CBibtexApp::PostErrorAlert(StringPtr inMessage) {
  387.  
  388.     ::ParamText(inMessage, "\p", "\p", "\p");
  389.     UDesktop::Deactivate();
  390.     (void) ::Alert(ALRT_OSErr, NULL);
  391.     UDesktop::Activate();
  392. }
  393.  
  394. /*************************************************************************
  395. *                                                                        *
  396. * Method:     CBibtexApp::OpenOptionsDialog                             *
  397. * Purpose:    Opens the options dialog.  Initializes the items in the    *
  398. *             dialog to reflect the current program settings.            *
  399. * Inputs:     None.                                                      *
  400. * Outputs:    None.                                                      *
  401. * Exceptions: None.                                                      *
  402. * Side Effects:   Displays a dialog.                                     *
  403. * Errors:     None.                                                      *
  404. * History:                                                               *
  405. *     1.0     28-Jul-95   RJZ.  Initial version.                         *
  406. *     1.0.1   30-Nov-95   RJZ.  Use dynamic_cast to cast pointers.  Use  *
  407. *                         Assert_ to assure that it worked.              *
  408. *                                                                        *
  409. *************************************************************************/
  410. void CBibtexApp::OpenOptionsDialog(void) {
  411.     LStdCheckBox    *checkBox;
  412.     Boolean         debugOn;
  413.     LDialogBox      *theDialog;
  414.     LUndoer         *undoer;
  415.  
  416.                                         // Create the dialog
  417.  
  418.     theDialog = (LDialogBox *) LWindow::CreateWindow(PPob_OptionsDialog, this);
  419.  
  420.                                         // make typing undoable
  421.     undoer = new LUndoer;
  422.     theDialog->AddAttachment(undoer);
  423.                                         // Get current Bibtex settings
  424.  
  425.     ::GetBibtexOptions(debugOn);
  426.  
  427.  
  428.                                         // Initialize debug box
  429.  
  430.     checkBox = dynamic_cast<LStdCheckBox *>
  431.         (theDialog->FindPaneByID(paneID_Debug));
  432.     Assert_(checkBox != NULL);
  433.     checkBox->SetValue(debugOn);
  434.                                         // Initialize Auto Quit option
  435.  
  436.     checkBox = dynamic_cast<LStdCheckBox *>
  437.         (theDialog->FindPaneByID(paneID_AutoQuit));
  438.     Assert_(checkBox != NULL);
  439.     checkBox->SetValue(mAutoQuit);
  440.                                         // Initialize Go On Open option
  441.  
  442.     checkBox = dynamic_cast<LStdCheckBox *>
  443.         (theDialog->FindPaneByID(paneID_GoOnOpen));
  444.     Assert_(checkBox != NULL);
  445.     checkBox->SetValue(mGoOnOpen);
  446.  
  447.     theDialog->Show();
  448. }
  449.  
  450. /*************************************************************************
  451. *                                                                        *
  452. * Method:     CBibtexApp::SetDialogOptions                              *
  453. * Purpose:    This function gets invoked when the user hits the OK       *
  454. *             button.  It extracts the selections the user has made and  *
  455. *             sets Bibtex variables accordingly.                      *
  456. * Inputs:     inPresponse is a pointer to a SDialogResponse structure.   *
  457. * Outputs:    None.                                                      *
  458. * Exceptions: None.                                                      *
  459. * Side Effects:   May change the value of some instance variables.       *
  460. * Errors:     None.                                                      *
  461. * History:                                                               *
  462. *     1.0     28-Jul-95   RJZ.  Initial version.                         *
  463. *     1.0.1   30-Nov-95   RJZ.  Use dynamic_cast to cast pointers.       *
  464. *                         Assert_ that it worked.                        *
  465. *                                                                        *
  466. *************************************************************************/
  467. void CBibtexApp::SetDialogOptions(SDialogResponse *inResponse) {
  468.     LStdCheckBox    *checkBox;
  469.     Boolean         debugOn;
  470.  
  471.                                         // Get value of debug box
  472.  
  473.     checkBox = dynamic_cast<LStdCheckBox *>
  474.         (inResponse->dialogBox->FindPaneByID(paneID_Debug));
  475.     Assert_(checkBox != NULL);
  476.     debugOn = checkBox->GetValue();
  477.                                         // Get value of auto quit box
  478.  
  479.     checkBox = dynamic_cast<LStdCheckBox *>
  480.         (inResponse->dialogBox->FindPaneByID(paneID_AutoQuit));
  481.     Assert_(checkBox != NULL);
  482.     mAutoQuit = checkBox->GetValue();
  483.                                         // Get value of Go On Open box
  484.  
  485.     checkBox = dynamic_cast<LStdCheckBox *>
  486.         (inResponse->dialogBox->FindPaneByID(paneID_GoOnOpen));
  487.     Assert_(checkBox != NULL);
  488.     mGoOnOpen = checkBox->GetValue();
  489.                                         // Set options
  490.  
  491.     ::SetBibtexOptions(debugOn);
  492.  
  493.                                         // Delete the dialog box
  494.     delete inResponse->dialogBox;
  495. }
  496.  
  497. /**************************************************************************
  498. *                                                                         *
  499. * Method:     CBibtexApp::AllowSubRemoval                                *
  500. * Purpose:    The window object invokes this method before closing the    *
  501. *             Log Window.  This allows us to do any cleaning up we need   *
  502. *             to do.                                                      *
  503. * Inputs:     inSub (I forget what it's for!)                             *
  504. * Outputs:    Returns true indicating that it's OK to close the window.   *
  505. * Exceptions: None.                                                       *
  506. * Side Effects:   Sets mLogWindow to indicate that the log window is no   *
  507. *             longer visible.  Also closes any files that might be open.  *
  508. * Errors:     None.                                                       *
  509. * History:                                                                *
  510. *     1.0     28-Jul-95   RJZ.  Initial version.                          *
  511. *                                                                         *
  512. **************************************************************************/
  513. Boolean CBibtexApp::AllowSubRemoval(LCommander *inSub) {
  514. #pragma unused (inSub)
  515.  
  516.     mLogWindow = NULL;
  517.     ::CloseAllFiles();
  518.  
  519.     return true;
  520. }
  521.  
  522. /************************************************************************
  523. *                                                                       *
  524. * Method:     CBibtexApp::MakeNewDocument                              *
  525. * Purpose:    This function gets invoked if the user wants to create a  *
  526. *             new document.  This is not a feature we support.          *
  527. * Inputs:     None.                                                     *
  528. * Outputs:    Just return NULL.                                         *
  529. * Exceptions: None.                                                     *
  530. * Side Effects:   None.                                                 *
  531. * Errors:     None.                                                     *
  532. * History:                                                              *
  533. *     1.0     28-Jul-95   RJZ.  Initial version.                        *
  534. *                                                                       *
  535. ************************************************************************/
  536. LModelObject *CBibtexApp::MakeNewDocument(void) {
  537.  
  538.     return NULL;
  539. }
  540.  
  541. /**************************************************************************
  542. *                                                                         *
  543. * Method:     CBibtexApp::ChooseDocument                                 *
  544. * Purpose:    Asks the user to select a document to open.  We filter the  *
  545. *             names so that only text files that end in ".aux" are        *
  546. *             shown.  If the user makes a selection, open the file.       *
  547. * Inputs:     None.                                                       *
  548. * Outputs:    None.                                                       *
  549. * Exceptions: None.                                                       *
  550. * Side Effects:   Displays a standard open file dialog on the screen.     *
  551. * Errors:     None.                                                       *
  552. * History:                                                                *
  553. *     1.0     28-Jul-95   RJZ.  Initial version.                          *
  554. *     1.0.1   27-Oct-95   RJZ.  Make the UPP a static data member.        *
  555. *                                                                         *
  556. **************************************************************************/
  557. void CBibtexApp::ChooseDocument(void) {
  558.     StandardFileReply   reply;
  559.     SFTypeList          typeList;
  560.  
  561.     typeList[0] = 'TEXT';
  562.     UDesktop::Deactivate();
  563.     ::StandardGetFile(sGetBtxFileFilter, 1, typeList, &reply);
  564.     UDesktop::Activate();
  565.     if (reply.sfGood)
  566.         OpenDocument(&reply.sfFile);
  567. }
  568.  
  569. /*************************************************************************
  570. *                                                                        *
  571. * Function:   CBibtexApp::GetBtxFileFilter (static)                     *
  572. * Purpose:    Filters file names for StandardGetFile so that the user    *
  573. *             will see only names that end in .aux.                      *
  574. * Inputs:     pb is a pointer to an information block for the name of a  *
  575. *             file that StandardGetFile is considering to display.       *
  576. * Outputs:    True if StandardGetFile should filter the file (not        *
  577. *             display it), false to display the file.                    *
  578. * Exceptions: None.                                                      *
  579. * Side Effects:   Affects which files are displayed by StandardGetFile.  *
  580. * Design:     If the file name is longer than 4 characters, assume that  *
  581. *             the last 4 characters contain an extension.                *
  582. * Errors:     None.                                                      *
  583. * History:                                                               *
  584. *     1.0     10-Jul-95   RJZ.  Initial version.                         *
  585. *     1.0.1   27-Oct-95   RJZ.  This is now a static member function.    *
  586. *                                                                        *
  587. *************************************************************************/
  588. pascal Boolean CBibtexApp::GetBtxFileFilter(CInfoPBPtr pb) {
  589.     char        *extensionPtr;
  590.     Boolean     filter;
  591.     StringPtr   name;
  592.  
  593.     name = pb->hFileInfo.ioNamePtr;
  594.     extensionPtr = (char *) name + StrLength(name) - 3;
  595.     if (StrLength(name) > 4 && strncmp(extensionPtr, BIBTEX_INP, 4) == 0)
  596.         filter = false;
  597.     else
  598.         filter = true;
  599.  
  600.     return filter;
  601. }
  602.  
  603. /**************************************************************************
  604. *                                                                         *
  605. * Method:     CBibtexApp::OpenDocument                                   *
  606. * Purpose:    Performs all processing associated with opening a           *
  607. *             document.  It tells Bibtex about the file.  If a window  *
  608. *             is already open, it clears out the text and changes the     *
  609. *             window title.  If no window is open, it creates a new one   *
  610. *             and displays it.                                            *
  611. * Inputs:     inMacFSSpec is a pointer to the FSSpec record of the file   *
  612. *             to be opened.                                               *
  613. * Outputs:    None.                                                       *
  614. * Exceptions: None.                                                       *
  615. * Side Effects:   Initializes some instance variables such as             *
  616. *             mGotBtxFile, mLogWindow, sTextEditPane, and sTextEditH.     *
  617. * Errors:     None.                                                       *
  618. * History:                                                                *
  619. *     1.0     28-Jul-95   RJZ.  Initial version.                          *
  620. *                                                                         *
  621. **************************************************************************/
  622. void CBibtexApp::OpenDocument(FSSpec *inMacFSSpec) {
  623.     Str255      windowTitle;
  624.  
  625.     ::CloseAllFiles();
  626.     ::IntroduceBtxFile(inMacFSSpec);
  627.     mGotBtxFile = true;
  628.     if (mLogWindow != NULL) {
  629.         ::TESetSelect(0L, 32767L, sTextEditH);
  630.         ::TEDelete(sTextEditH);
  631.         ::GetWindowTitle(windowTitle);
  632.         mLogWindow->SetDescriptor(windowTitle);
  633.         mLogWindow->Refresh();
  634.     } else {
  635.         mLogWindow = LWindow::CreateWindow(PPob_EditWindow, this);
  636.         ::GetWindowTitle(windowTitle);
  637.         mLogWindow->SetDescriptor(windowTitle);
  638.         mLogWindow->Show();
  639.         sTextEditPane = (LTextEdit *)
  640.                         mLogWindow->FindPaneByID(paneID_TextEdit);
  641.         sTextEditH = sTextEditPane->GetMacTEH();
  642.     }
  643.     if (mGoOnOpen)
  644.         ObeyCommand(cmd_Go, NULL);
  645. }
  646.  
  647. /***********************************************************************
  648. *                                                                      *
  649. * Method:     CBibtexApp::DoSaveSettings                              *
  650. * Purpose:    This function gets invoked when the user selects Save    *
  651. *             Current Settings from the Options menu.  It saves the    *
  652. *             current settings in the preferences file.  If the        *
  653. *             preferences file doesn't exist, it creates it.           *
  654. * Inputs:     None.                                                    *
  655. * Outputs:    None.                                                    *
  656. * Exceptions: None.                                                    *
  657. * Side Effects:   Write the current settings to the preferences file.  *
  658. *             May also create a preferences file.                      *
  659. * Errors:     None.                                                    *
  660. * History:                                                             *
  661. *     1.0     28-Jul-95   RJZ.  Initial version.                       *
  662. *                                                                      *
  663. ***********************************************************************/
  664. void CBibtexApp::DoSaveSettings(void) {
  665.     Boolean         debugOn;
  666.     OSErr           err;
  667.     short           fileRefNum;
  668.  
  669.     if (!mPrefsFileExists)
  670.         CreatePrefsFile();
  671.                                         // Get current Bibtex settings
  672.     if (mPrefsFileExists) {
  673.         ::GetBibtexOptions(debugOn);
  674.         err = ::OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
  675.         if (err == noErr) {
  676.             WriteBooleanPref(debugOn,        myBLID_Debug,        fileRefNum);
  677.             WriteBooleanPref(mGoOnOpen,      myBLID_GoOnOpen,     fileRefNum);
  678.             WriteBooleanPref(mAutoQuit,      myBLID_AutoQuit,     fileRefNum);
  679.             err = ::ClosePreferencesFile(fileRefNum);
  680.         } else
  681.             ::ReportOSErr("Error trying to open preferences file: %s", err);
  682.     }
  683. }
  684.  
  685. /**************************************************************************
  686. *                                                                         *
  687. * Method:     CBibtexApp::CreatePrefsFile                                *
  688. * Purpose:    Creates a preferences file.                                 *
  689. * Inputs:     None.                                                       *
  690. * Outputs:    None.                                                       *
  691. * Exceptions: None.                                                       *
  692. * Side Effects:   Creates a preferences file.                             *
  693. * Design:     This function uses the StdPrefsLib to do all the right      *
  694. *             things.  It follows the guidelines in Develop 18's article  *
  695. *             "The Right Way to Implement Preferences Files."  It even    *
  696. *             creates two vers resources for the file and inserts the     *
  697. *             application-missing resource ('STR ' -16397).               *
  698. * Errors:     Posts an alert if there are any troubles creating the       *
  699. *             file.                                                       *
  700. * History:                                                                *
  701. *     1.0     28-Jul-95   RJZ.  Initial version.                          *
  702. *                                                                         *
  703. **************************************************************************/
  704. void CBibtexApp::CreatePrefsFile(void) {
  705.     Str255          companyStr = kCompanyString;
  706.     OSErr           err;
  707.     short           fileRefNum;
  708.     Str255          fileSetStr = kFileSetString;
  709.     NumVersion      numVersion;
  710.     short           regionCode;
  711.     Str255          versNumStr = kVersNumString;
  712.  
  713.     err = ::NewPreferencesFile(kDummyCreator, kDummyType,
  714.                                kPrefFileName, NULL, kIdentString);
  715.     if (err == noErr) {
  716.         mPrefsFileExists = true;
  717.         numVersion.majorRev = kMajorRevision;
  718.         numVersion.minorAndBugRev = kMinorAndBugRevision;
  719.         numVersion.stage = kStage;
  720.         numVersion.nonRelRev = kNonReleaseRevision;
  721.         regionCode = ::GetScriptManagerVariable(smRegionCode);
  722.         err = ::OpenPreferencesFile(kDummyCreator, kDummyType, &fileRefNum);
  723.     }
  724.                                         // Set the contents of the vers 1
  725.                                         // resource
  726.     if (err == noErr)
  727.         err = ::SetPreferencesFileVersion(fileRefNum, kVers1, &numVersion,
  728.                                           regionCode, versNumStr, companyStr);
  729.  
  730.                                         // Set the contents of the vers 2
  731.                                         // resource
  732.     if (err == noErr)
  733.         err = ::SetPreferencesFileVersion(fileRefNum, kVers2, &numVersion,
  734.                                           regionCode, versNumStr, fileSetStr);
  735.  
  736.                                         // Post an alert if error
  737.     if (err != noErr)
  738.         ::ReportOSErr("Error trying to create Preferences file: %s", err);
  739.  
  740.                                         // Close prefs file
  741.  
  742.     err = ::ClosePreferencesFile(fileRefNum);
  743. }
  744.  
  745. /*************************************************************************
  746. *                                                                        *
  747. * Method:     CBibtexApp::WriteBooleanPref                              *
  748. * Purpose:    Writes a Boolean preference to the preferences file.       *
  749. *             Boolean preferences are stored in myBL resources.          *
  750. * Inputs:     inValue is the value to be saved.  inID the the id of the  *
  751. *             myBL resource to use.                                      *
  752. * Outputs:    None.                                                      *
  753. * Exceptions: None.                                                      *
  754. * Side Effects:   Writes to the preferences file.                        *
  755. * Errors:     Posts an alert if there was trouble writing.               *
  756. * History:                                                               *
  757. *     1.0     28-Jul-95   RJZ.  Initial version.                         *
  758. *                                                                        *
  759. *************************************************************************/
  760. void CBibtexApp::WriteBooleanPref(Boolean  inValue,
  761.                                    short    inID,
  762.                                    short    inFileRefNum)
  763. {
  764.     OSErr   err = noErr;
  765.     Handle  prefHandle;
  766.     short   resID;
  767.  
  768.     resID = inID;
  769.                                         // Create a handle for our preference
  770.                                         // data
  771.  
  772.     prefHandle = ::NewHandle(sizeof(char));
  773.     if (prefHandle == NULL)
  774.         err = ::MemError();
  775.                                         // Set the contents of the preference
  776.                                         // data
  777.     if (err == noErr) {
  778.         **((char **) prefHandle) = inValue;
  779.  
  780.                                         // Write the preference data to prefs
  781.                                         // file
  782.  
  783.         err = ::WritePreference(inFileRefNum, 'myBL', &resID, prefHandle);
  784.     }
  785.                                         // Post an alert if error
  786.     if (err != noErr)
  787.         ::ReportOSErr("Error trying to write Preferences file: %s", err);
  788.  
  789.                                         // clean up
  790.     ::DisposeHandle(prefHandle);
  791. }
  792.  
  793. /**************************************************************************
  794. *                                                                         *
  795. * Method:     CBibtexApp::WriteLongPref                                  *
  796. * Purpose:    Writes a long preference to the preferences file            *
  797. * Inputs:     inValue is the value to be written.  inID is the id of the  *
  798. *             myLN resource to use.                                       *
  799. * Outputs:    None.                                                       *
  800. * Exceptions: None.                                                       *
  801. * Side Effects:   Writes to the preferences file.                         *
  802. * Errors:     Posts an alert if there is an error.                        *
  803. * History:                                                                *
  804. *     1.0     28-Jul-95   RJZ.  Initial version.                          *
  805. *                                                                         *
  806. **************************************************************************/
  807. void CBibtexApp::WriteLongPref(long    inValue,
  808.                                 short   inID,
  809.                                 short   inFileRefNum)
  810. {
  811.     OSErr   err = noErr;
  812.     Handle  prefHandle;
  813.     short   resID;
  814.  
  815.     resID = inID;
  816.                                         // Create a handle for our preference
  817.                                         // data
  818.  
  819.     prefHandle = ::NewHandle(sizeof(long));
  820.     if (prefHandle == NULL)
  821.         err = ::MemError();
  822.                                         // Set the contents of the preference
  823.                                         // data
  824.     if (err == noErr) {
  825.         **((long **) prefHandle) = inValue;
  826.  
  827.                                         // Write the preference data to prefs
  828.                                         // file
  829.  
  830.         err = ::WritePreference(inFileRefNum, 'myLN', &resID, prefHandle);
  831.     }
  832.                                         // Post an alert if error
  833.     if (err != noErr)
  834.         ::ReportOSErr("Error trying to write Preferences file: %s", err);
  835.  
  836.                                         // clean up
  837.     ::DisposeHandle(prefHandle);
  838. }
  839.  
  840. /*************************************************************************
  841. *                                                                        *
  842. * Method:     CBibtexApp::WriteStringPref                               *
  843. * Purpose:    Writes a string preference to the preferences file.        *
  844. * Inputs:     inValue is a pointer to a Pascal style string to be        *
  845. *             written.  It may be any length up to 255.  inID is the id  *
  846. *             of the myST resource to use.                               *
  847. * Outputs:    None.                                                      *
  848. * Exceptions: None.                                                      *
  849. * Side Effects:   Writes to the preferences file.                        *
  850. * Errors:     Posts an alert if there is an error.                       *
  851. * History:                                                               *
  852. *     1.0     28-Jul-95   RJZ.  Initial version.                         *
  853. *                                                                        *
  854. *************************************************************************/
  855. void CBibtexApp::WriteStringPref(StringPtr inValue,
  856.                                   short     inID,
  857.                                   short     inFileRefNum)
  858. {
  859.     Size    byteCount;
  860.     OSErr   err = noErr;
  861.     Handle  prefHandle;
  862.     short   resID;
  863.  
  864.     resID = inID;
  865.                                         // Create a handle for our preference
  866.                                         // data
  867.  
  868.     prefHandle = ::NewHandleClear(sizeof(Str255));
  869.     if (prefHandle == NULL)
  870.         err = ::MemError();
  871.                                         // Set the contents of the preference
  872.                                         // data.  It\'s probably not necessary
  873.                                         // to lock the handle.  Be safe though.
  874.     if (err == noErr) {
  875.         byteCount = inValue[0] + 1;
  876.         ::HLock(prefHandle);
  877.         ::BlockMoveData(inValue, *prefHandle, byteCount);
  878.         ::HUnlock(prefHandle);
  879.                                         // Write the preference data to prefs
  880.                                         // file
  881.  
  882.         err = ::WritePreference(inFileRefNum, 'myST', &resID, prefHandle);
  883.     }
  884.                                         // Post an alert if error
  885.     if (err != noErr)
  886.         ::ReportOSErr("Error trying to write Preferences file: %s", err);
  887.  
  888.                                         // clean up
  889.     ::DisposeHandle(prefHandle);
  890. }
  891.  
  892. /*********************************************************************
  893. *                                                                    *
  894. * Method:     CBibtexApp::ReadBooleanPref                           *
  895. * Purpose:    Reads a Boolean preference from the preferences file.  *
  896. * Inputs:     inID is the id of the myBL resource to read.           *
  897. * Outputs:    outValue is the value of the resource.                 *
  898. * Exceptions: None.                                                  *
  899. * Side Effects:   None.                                              *
  900. * Errors:     Posts an alert if there is an error.                   *
  901. * History:                                                           *
  902. *     1.0     28-Jul-95   RJZ.  Initial version.                     *
  903. *                                                                    *
  904. *********************************************************************/
  905. void CBibtexApp::ReadBooleanPref(Boolean&  outValue,
  906.                                   short     inID,
  907.                                   short     inFileRefNum)
  908. {
  909.     OSErr   err;
  910.     Handle  prefHandle;
  911.     short   resID;
  912.  
  913.     resID = inID;
  914.                                         //  Read the resource
  915.  
  916.     err = ::ReadPreference(inFileRefNum, 'myBL', &resID, &prefHandle);
  917.  
  918.                                         // Post an alert if error
  919.     if (err != noErr)
  920.         ::ReportOSErr("Error trying to read Preferences file: %s", err);
  921.  
  922.     outValue = **prefHandle != 0;
  923.     ::DisposeHandle(prefHandle);
  924. }
  925.  
  926. /***************************************************************
  927. *                                                              *
  928. * Method:     CBibtexApp::ReadLongPref                        *
  929. * Purpose:    Reads a long value from a myLN resource.         *
  930. * Inputs:     inID is the id of the myLN resource to read.     *
  931. * Outputs:    outValue is the value read.                      *
  932. * Exceptions: None.                                            *
  933. * Side Effects:   None.                                        *
  934. * Errors:     Posts an alert if there is any trouble reading.  *
  935. * History:                                                     *
  936. *     1.0     28-Jul-95   RJZ.  Initial version.               *
  937. *                                                              *
  938. ***************************************************************/
  939. void CBibtexApp::ReadLongPref(long&    outValue,
  940.                                short    inID,
  941.                                short    inFileRefNum)
  942. {
  943.     OSErr   err;
  944.     Handle  prefHandle;
  945.     short   resID;
  946.  
  947.     resID = inID;
  948.                                         //  Read the resource
  949.  
  950.     err = ::ReadPreference(inFileRefNum, 'myLN', &resID, &prefHandle);
  951.  
  952.                                         // Post an alert if error
  953.     if (err != noErr)
  954.         ::ReportOSErr("Error trying to read Preferences file: %s", err);
  955.  
  956.     outValue = **((long **) prefHandle);
  957.     ::DisposeHandle(prefHandle);
  958. }
  959.  
  960. /*******************************************************************
  961. *                                                                  *
  962. * Method:     CBibtexApp::ReadStringPref                          *
  963. * Purpose:    Reads a string preference from the preference file.  *
  964. * Inputs:     inID is the id of the myST resource to read.         *
  965. * Outputs:    outValue points to the value read.                   *
  966. * Exceptions: None                                                 *
  967. * Side Effects:   None.                                            *
  968. * Errors:     Posts an alert if there is a read error.             *
  969. * History:                                                         *
  970. *     1.0     28-Jul-95   RJZ.  Initial version.                   *
  971. *                                                                  *
  972. *******************************************************************/
  973. void CBibtexApp::ReadStringPref(StringPtr  outValue,
  974.                                  short      inID,
  975.                                  short      inFileRefNum)
  976. {
  977.     Size    byteCount;
  978.     OSErr   err;
  979.     Handle  prefHandle;
  980.     short   resID;
  981.  
  982.     resID = inID;
  983.                                         //  Read the resource
  984.  
  985.     err = ::ReadPreference(inFileRefNum, 'myST', &resID, &prefHandle);
  986.  
  987.                                         // Post an alert if error
  988.     if (err != noErr)
  989.         ::ReportOSErr("Error trying to read Preferences file: %s", err);
  990.  
  991.     ::HLock(prefHandle);
  992.     byteCount = **prefHandle + 1;
  993.     ::BlockMoveData(*prefHandle, outValue, byteCount);
  994.  
  995.     ::DisposeHandle(prefHandle);
  996. }
  997.  
  998. #pragma profile reset
  999.